﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;

namespace StatusPlan3.UC
{
    /// <summary>
    /// Interaction logic for UCProject.xaml
    /// </summary>
    public partial class UCProject : UserControl
    {
        public ProjectGroup projectGroup;
        public RadPane rp;

        public UCProject()
        {
            InitializeComponent();
        }

        public void showNew(ProjectGroup pg)
        {
            TextBlock_ProjectGroupName.Text = pg.name;
            TextBlock_Type.Text = "Stovna verkætlan";
            projectGroup = pg;
            ComboBox_Responsible.ItemsSource = projectGroup.projectMembers;
            ComboBox_Responsible.DisplayMemberPath = "fullName";

        }

        private Boolean validateFields()
        {
            Boolean ok = true;
            String errorString = "";
            if (TextBox_Name.Text.Trim().Length == 0)
            {
                errorString += "Navn feltið kann ikki verða tømt";
                ok = false;
            }

            if (Estimated.Text.Trim().Length > 0)
            {
                try
                {
                    Decimal.Parse(Estimated.Text);
                }
                catch
                {
                    errorString += "\nÆtla tíð skal verða tøl";
                    ok = false;
                }
            }

            
            if (!ok)
                MessageBox.Show(errorString, "Feilur: Stovna", MessageBoxButton.OK, MessageBoxImage.Error);

            return ok;
        }

        private void createProject()
        {
            if (validateFields())
            {
                Project p = new Project();
                p.name = TextBox_Name.Text.Trim();
                
                if(ComboBox_Responsible.SelectedItem != null)
                    p.responsible = ComboBox_Responsible.SelectedItem as User;

                if (DatePicker_StartDate.SelectedDate != null)
                    p.startDate = DatePicker_StartDate.SelectedDate.Value;

                if (DatePicker_Deadline.SelectedDate != null)
                    p.deadline = DatePicker_Deadline.SelectedDate.Value;

                p.createdBy = StatusPlanData.currentUser;
                p.createdDate = DateTime.Now;

                try
                {
                    Decimal estimatedHours = Decimal.Parse(Estimated.Text);
                }
                catch { }

                try
                {
                    StatusPlanData.statusPlanHandler.saveProject(p, projectGroup.id);
                    MessageBox.Show("Verkætlan: " + p.name + " er stovnað", "Stovna verkætlan", MessageBoxButton.OK, MessageBoxImage.Information);
                    StatusPlanData.mainWindow.refreshProjectGroup(projectGroup.id);
                    rp.IsHidden = true;
                }
                catch (Exception e)
                {
                    MessageBox.Show("Feilur " + e.Message + "\n\nInner: " + e.InnerException, "Feilur: Stovna verkætlan", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }

        private void RadButton_Click(object sender, RoutedEventArgs e)
        {
            createProject();
        }

        private void RadButton_Click_1(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("Ert tú vís/ur í at tú vilt tøma øll felt?", "Tøm", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
            {
                TextBox_Name.Text = "";
                ComboBox_Responsible.SelectedItem = null;
                DatePicker_Deadline.SelectedDate = null;
                DatePicker_StartDate.SelectedDate = null;
                Estimated.Text = "";
            }
        }
    }
}
